package com.skstudio.reportview;

import com.skstudio.report.ReportNetwork;
import com.skstudio.report.ReportNetwork.DataNetwork;
import com.skstudio.report.SpyData;

public class ReporterNetworkThrput extends ReportViewController {

    ReportNetwork mNet = null;

    @Override
    public void setSpyData(SpyData ref) {
        super.setSpyData(ref);
        mNet = ref.getReportNet();
    }

    @Override
    public String stringYLabels(int id) {
        String text = "0";
        switch (id) {
            case 0:
                text = "0KiB/s";
                break;
            case 1:
                text = "10KiB/s";
                break;
            case 2:
                text = "50KiB/s";
                break;
            case 3:
                text = "100KiB/s";
                break;
            case 4:
                text = "250KiB/s";
                break;
            case 5:
                text = "500KiB/s";
                break;
            case 6:
                text = "1MiB/s";
                break;
            case 7:
                text = "2MiB/s";
                break;
            case 8:
                text = "5MiB/s";
                break;
            case 9:
                text = "10MiB/s";
                break;
            case 10:
                text = "50MiB/s";
                break;
            default:
                break;
        }
        return text;
    }

    @Override
    public int getReportRows() {
        return 2;
    }

    @Override
    public int getReportColumns() {
        return mNet.mDataList.size();
    }

    @Override
    public int getEntry(int row, int col) {
        if (getReportRows() > row && mNet.mDataList.size() > col) {
            DataNetwork dc = mNet.mDataList.get(col);
            switch (row) {
                case 0:
                    return (int) calcPosition(dc.mRxRate);
                case 1:
                    return (int) calcPosition(dc.mTxRate);
            }
            return 0;
        }
        return 0;
    }

    protected float calcPosition(float nRate) {
        if (nRate < 0) {
            return 0.f;
        }
        if (nRate < 10 * 1024) {
            return 10 * (nRate / (10 * 1024));
        } else if (nRate < 50 * 1024) {
            return 10 + 10 * ((nRate - 10 * 1024) / ((50 - 10) * 1024));
        } else if (nRate < 100 * 1024) {
            return 20 + 10 * ((nRate - 50 * 1024) / ((100 - 50) * 1024));
        } else if (nRate < 250 * 1024) {
            return 30 + 10 * ((nRate - 100 * 1024) / ((250 - 100) * 1024));
        } else if (nRate < 500 * 1024) {
            return 40 + 10 * ((nRate - 250 * 1024) / ((500 - 250) * 1024));
        } else if (nRate < 1024 * 1024) {
            return 50 + 10 * ((nRate - 500 * 1024) / ((1024 - 500) * 1024));
        } else if (nRate < 2 * 1024 * 1024) {
            return 60 + 10 * ((nRate - 1024 * 1024) / ((2 * 1024 - 1024) * 1024));
        } else if (nRate < 5 * 1024 * 1024) {
            return 70 + 10 * ((nRate - 2 * 1024 * 1024) / ((5 * 1024 - 2 * 1024) * 1024));
        } else if (nRate < 10 * 1024 * 1024) {
            return 80 + 10 * ((nRate - 5 * 1024 * 1024) / ((10 * 1024 - 5 * 1024) * 1024));
        } else if (nRate < 50 * 1024 * 1024) {
            return 90 + 10 * ((nRate - 10 * 1024 * 1024) / ((50 * 1024 - 10 * 1024) * 1024));
        }
        return 0.f;
    }

    @Override
    public String getReportDescription() {
        return "Wifi Usage";
    }

    @Override
    public String getEntryName(int row) {
        switch (row) {
            case 0:
                return "The amount of data received per second";
            case 1:
                return "The amount of data sent per second";
        }
        return null;
    }

    @Override
    public String getEntryHint(int col) {
        if (mNet.mDataList.size() > col) {
            ReportNetwork.DataNetwork dc = mNet.mDataList.get(col);
            String hint = String.format("Received %sk, Transmitted %sk",
                    dc.mRx.shiftRight(10).toString(), dc.mTx.shiftRight(10).toString());
            return hint;
        }
        return null;
    }
}
